home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / DJGPP / LGP250S1.ZIP / src / libgplus.5 / libgplus / tests / tvec.cc < prev    next >
C/C++ Source or Header  |  1993-06-06  |  2KB  |  96 lines

  1. /*
  2.  test/demo of Vecs, AVecs
  3. */
  4.  
  5. #include <stream.h>
  6. #include "iVec.h"
  7. #include "iAVec.h"
  8.  
  9. int int_compare(int a, int b)
  10. {
  11.   return a - b;
  12. }
  13.  
  14. int plus(int a, int b)
  15. {
  16.   return a + b;
  17. }
  18.  
  19. int inc(int a)
  20. {
  21.   return a + 1;
  22. }
  23.  
  24. void printint(int a)
  25. {
  26.   cout << a << " ";
  27. }
  28.  
  29. void print(intVec& a)
  30. {
  31.   a.apply(printint);
  32.   cout << "\n";
  33. }
  34.  
  35. #include <MLCG.h>
  36.  
  37. MLCG randgen;
  38.     
  39. int main()
  40. {
  41.   intVec a(20);
  42.   for (int i = 0; i < a.capacity(); ++i) a[i] = randgen.asLong() % 100;
  43.   cout << "a: "; print(a);
  44.   a.sort(int_compare);
  45.   cout << "a.sort():"; print(a);
  46.   intVec b = map(inc, a);
  47.   cout << "b = map(inc, a): "; print(b);
  48.   intVec c = merge(a, b, int_compare);
  49.   cout << "c = merge(a, b): "; print(c);
  50.   intVec d = concat(a, b);
  51.   cout << "d = concat(a, b): "; print(d);
  52.   d.resize(10);
  53.   cout << "d.resize(10): "; print(d);
  54.   d.reverse();
  55.   cout << "d.reverse(): "; print(d);
  56.   d.fill(0, 4, 4);
  57.   cout << "d.fill(0, 4, 4): "; print(d);
  58.   cout << "d.reduce(plus, 0) = " <<   d.reduce(plus, 0) << "\n";
  59.   intVec e = d.at(2, 5);
  60.   cout << "e = d.at(2, 5): "; print(e);
  61.  
  62.   intAVec x(20);
  63.   for (i = 0; i < x.capacity(); ++i) x[i] = i;
  64.   cout << "x: "; print(x);
  65.   intAVec y(20);
  66.   for (i = 0; i < y.capacity(); ++i) y[i] = randgen.asLong() % 100 + 1;
  67.   cout << "y: "; print(y);
  68.  
  69.   cout << "x + y: "; print(x + y);
  70.   cout << "x - y: "; print(x - y);
  71.   cout << "product(x, y): "; print(product(x,y));
  72.   cout << "quotient(x, y): "; print(quotient(x,y));
  73.   cout << "x * y: " << (x * y) << "\n";
  74.  
  75.   cout << "x + 2: "; print(x + 2);
  76.   cout << "x - 2: "; print(x - 2);
  77.   cout << "x * 2: "; print(x * 2);
  78.   cout << "x / 2: "; print(x / 2);
  79.  
  80.   intAVec z(20, 1);
  81.   cout << "z(20, 1): "; print(z);
  82.   cout << "z = -z: "; print(z = -z);
  83.   cout << "z += x: "; print(z += x);
  84.   cout << "z -= x: "; print(z -= x);
  85.  
  86.   cout << "x.sum(): " << x.sum() << "\n";
  87.   cout << "x.sumsq(): " << x.sumsq() << "\n";
  88.   cout << "x.min(): " << x.min() << "\n";
  89.   cout << "x.max(): " << x.max() << "\n";
  90.   cout << "x.min_index(): " << x.min_index() << "\n";
  91.   cout << "x.max_index(): " << x.max_index() << "\n";
  92.  
  93.   cout << "\nEnd of test\n";
  94.   return 0;
  95. }
  96.